package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.core.Result;
import com.dongdongshop.core.ResultEnum;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.service.BrandService;
import com.dongdongshop.service.SpecificationService;
import com.dongdongshop.service.TemplateServcie;
import com.dongdongshop.util.PageBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("templateController")
public class TemplateController {
    //开启日志
    Logger logger = LoggerFactory.getLogger(TemplateController.class);

    @Reference(check=false ,timeout=106666)
    private BrandService brandService;
    @Reference(check=false ,timeout=106666)
    private SpecificationService specificationService;
    @Reference(check=false ,timeout=106666)
    private TemplateServcie templateServcie;

    //跳转到模板展示页面
    @RequestMapping("templateList")
    public String templateList(Model model){
        //查询品牌表所有数据
        List<TbBrand> brands = brandService.getBrandAllInfo();
        model.addAttribute("brands",brands);
        //查询规格表所有数据
        List<TbSpecification> specs = specificationService.getSpecAllInfo();
        model.addAttribute("specs",specs);
        return "admin/type_template";
    }

    //查询模板信息
    @RequestMapping("getTemplate")
    @ResponseBody
    public Result getTemplate(String name, @RequestParam(defaultValue = "1") Integer pageNumber, @RequestParam(defaultValue = "2") Integer pageSize){
        try {
            logger.info("查询方法开始运行{}");
            PageBean<TbTypeTemplate> templatePageBean = templateServcie.getTemplate(name,pageNumber,pageSize);
            return Result.response(ResultEnum.SUCCESS).setData(templatePageBean);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }

    //增加模板
    @RequestMapping("insertTemplate")
    @ResponseBody
    public Result insertTemplate(TbTypeTemplate template){
        try {
            logger.info("增加方法开始运行{}");
            templateServcie.insertTemplate(template);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }

    //批量删除
    @RequestMapping("deleteBatch")
    @ResponseBody
    public Result deleteBatch(@RequestParam("ids[]")Integer[] ids){
        try {
            logger.info("删除方法开始运行{}");
            templateServcie.deleteBatch(ids);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }

    //修改回显
    @RequestMapping("updateInfo")
    @ResponseBody
    public Result updateInfo(Integer id){
        try {
            logger.info("回显方法开始运行{}");
            TbTypeTemplate template = templateServcie.updateInfo(id);
            return Result.response(ResultEnum.SUCCESS).setData(template);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }

    //修改
    @RequestMapping("update")
    @ResponseBody
    public Result update(TbTypeTemplate template){
        try {
            logger.info("修改方法开始运行{}");
            templateServcie.update(template);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }
}
